<template>
	<view class="container">
		<uni-card v-for="(item,index) in commentlist" :title="item.userId+''" thumbnail="" :extra="item.postDate"
			:note="item.foodId">
			{{item.comment}}
		</uni-card>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				"foodid": 1,
				"pageIndex": 1,
				"pageSize": 10,
				"commentlist": [{
					"id": 48,
					"comment": "1111",
					"foodId": 1,
					"postDate": "2024-03-15",
					"userId": 22
				}]

			}
		},
		onPullDownRefresh() {
			this.reload()
		},
		onReachBottom() {
			uni.showLoading({
				title: '加载中...',
				mask: false
			});
			setTimeout(() => {
				this.loadmore()
				uni.hideLoading()
			}, 1000)

		},
		methods: {
			reload() {
				uni.request({
					url: 'http://localhost:8090/foodapp/comments/getCommentsByFoodIdByPage',
					method: 'POST',
					data: {
						"foodid": this.foodid,
						"pageIndex": 0,
						"pageSize": this.pageSize
					},
					success: res => {
						console.log(res);
						// 将数据拼接在一起显示
						this.commentlist = res.data.data.content
					},
					fail: () => {},
					complete: () => {
						uni.stopPullDownRefresh()
					}
				});
			},
			// 加载新的一页，并合并到原有数据中
			loadmore() {
				this.pageIndex = this.pageIndex + 1
				uni.request({
					url: 'http://localhost:8090/foodapp/comments/getCommentsByFoodIdByPage',
					method: 'POST',
					data: {
						"foodid": this.foodid,
						"pageIndex": this.pageIndex,
						"pageSize": this.pageSize
					},
					success: res => {
						console.log(res);
						// 将数据拼接在一起显示
						this.commentlist = [...this.commentlist, ...res.data.data.content]
					},
					fail: () => {},
					complete: () => {}
				});
			}
		},
		onLoad(option) {
			this.foodid = option.foodid || 1
			this.pageIndex = 0
			this.reload()
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>